<template>
	<div class="photo-page">
		<div class="header">
			<button class="cancel-button" @click="goBack">取消</button>
			<button class="publish-button" @click="publish">发表</button>
		</div>
		<textarea placeholder="这一刻的想法..." v-model="inputText"></textarea>
		<div class="image-container">
			<!-- <div class="image-item" v-for="(image, index) in imageList" :key="index">
				<image :src="image" mode="aspectFill"></image>
			</div> -->
			<div class="image-item">
				<image :src="img" mode="aspectFill" class="avatar-image"></image>
			</div>
			<div class="add-image-item" @click="chooseAvatar">
				<view class="plus-icon">+</view>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		uploadImg
	} from "@/api/system/common.js";
	import {
		savecommunity
	} from "@/api/community.js";
	import config from '@/config';
	export default {
		data() {
			return {
				imageList: [],
				img: "",
				fileName: "",
				inputText: ""
			};
		},
		methods: {
			addImage() {
				const tempImagePath = 'file:///D:/软件工程导论/青年/static/章若楠.png';
				this.imageList.push(tempImagePath);
			},
			publish() {
				const data = {
					img: this.fileName,
					content: this.inputText
				}
				savecommunity(data).then(res => {
					uni.showToast({
						title: '发表成功',
						icon: 'success',
						duration: 1500,
						style: `
					        border: 1px solid #6699cc;
					        background-color: #e6f2ff;
					        color: #333;
					    `
					});
				})
				this.inputText = ''
				this.fileName = ''
				this.img = ''
			},
			goBack() {
				uni.navigateBack();
			},
			chooseAvatar() {
				uni.chooseImage({
					count: 1,
					// 获取图片本地路径
					success: (res) => {
						const tempFilePaths = res.tempFilePaths
						const tempFiles = res.tempFiles
						const data = {
							filePath: tempFilePaths[0],
						}
						uploadImg(data).then(res => {
							console.log(res)
							// 上传成功后做回显操作
							this.img = config.baseUrl + res.fileName;
							this.fileName = res.fileName;
						})
					}
				})
			}
		}
	};
</script>

<style scoped>
	.photo-page {
		padding: 20px;
		background-image: radial-gradient(circle at center, #ffffff, #b3e5fc, #f9fbe7);
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.header {
		width: 100%;
		display: flex;
		justify-content: space-between;
		padding: 0 20px;
		margin-bottom: 20px;
	}

	button {
		padding: 6px 12px;
		border: none;
		border-radius: 4px;
		font-size: 12px;
		cursor: pointer;
	}

	.cancel-button {
		background-color: #81d4fa;
		color: #333;
	}

	.publish-button {
		background-color: #007aff;
		color: white;
	}

	.text-container {
		width: 100%;
		margin-bottom: 20px;
	}

	textarea {
		width: 80%;
		height: 80px;
		padding: 10px;
		border: 1px solid #81d4fa;
		border-radius: 10px;
		background-color: #f8f9fa;
		box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
		margin: 0 auto;
	}

	.image-container {
		display: flex;
		flex-wrap: wrap;
	}

	.image-item {
		width: 80px;
		height: 80px;
		margin-right: 10px;
		margin-bottom: 10px;
		overflow: hidden;
		border-radius: 5px;
		box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
	}

	.add-image-item {
		width: 80px;
		height: 80px;
		margin-right: 10px;
		margin-bottom: 10px;
		background-color: #e6f2ff;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 5px;
		cursor: pointer;
	}

	.plus-icon {
		font-size: 24px;
		color: #007aff;
	}
</style>